#define _CRT_SECURE_NO_WARNINGS 1


 // Definition for singly-linked list.
 struct ListNode 
{
      int val;
      struct ListNode *next;
 };
 
struct ListNode* removeElements(struct ListNode* head, int val) {
    struct ListNode* tmp = head;
    struct ListNode* x;
    if (head == NULL)
    {
        return head;
    }

    while (tmp->next || tmp->val == val)
    {
        if (tmp == head && tmp->val == val)
        {
            head = tmp->next;
            free(tmp);
            tmp = head;
            if (tmp == NULL)
                return head;
        }
        else if (tmp->next->val == val)
        {
            x = tmp->next;
            tmp->next = tmp->next->next;
            free(x);
        }
        else
        {
            tmp = tmp->next;
        }
    }

    return head;
}